package cn.tedu.flower.manage.content.service.impl;

import cn.tedu.flower.manage.common.ex.ServiceException;
import cn.tedu.flower.manage.common.web.ServiceCode;
import cn.tedu.flower.manage.content.dao.persist.repository.IAftermarketRepository;
import cn.tedu.flower.manage.content.pojo.entity.Aftermarket;
import cn.tedu.flower.manage.content.pojo.param.AftermarketAddNewParam;
import cn.tedu.flower.manage.content.pojo.param.AftermarketUpdateInfoParam;
import cn.tedu.flower.manage.content.pojo.vo.AftermarketListItemVO;
import cn.tedu.flower.manage.content.pojo.vo.AftermarketStandardVO;
import cn.tedu.flower.manage.content.service.IAftermarketService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 处理类别的业务实现类
 *
 * @author java@tedu.cn
 * @version 1.0
 */
@Slf4j
@Service
public class AftermarketServiceImpl implements IAftermarketService {

    @Value("${flower-store.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;

    @Autowired
    private IAftermarketRepository aftermarketRepository;

    public AftermarketServiceImpl() {
        log.debug("创建业务类：AftermarketServiceImpl");
    }

    @Override
    public void addNew(AftermarketAddNewParam aftermarketAddNewParam) {
        log.debug("开始处理【添加类别】的业务，参数：{}", aftermarketAddNewParam);
        String name = aftermarketAddNewParam.getNickName();
        Long id = aftermarketAddNewParam.getId();
        int count = aftermarketRepository.countByName(name);
        log.debug("根据名称【{}】统计数量，结果：{}", name, count);
        Aftermarket aftermarket = new Aftermarket();
        BeanUtils.copyProperties(aftermarketAddNewParam,aftermarket);

        int rows = aftermarketRepository.insert(aftermarket);
        if (rows != 1){
            String message = "添加售后问题失败，服务器忙，请稍后尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }

    }

    @Override
    public int insertBatch(List<Aftermarket> aftermarketList) {
        return 0;
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【根据ID删除售后】的业务，参数：{}", id);
        AftermarketStandardVO currentAftermarket =aftermarketRepository.getStandardById(id);
        if (currentAftermarket == null){
            String message = "删除售后失败，尝试删除的售后数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);

        }
        int rows = aftermarketRepository.deleteById(id);
        if (rows !=1){
            String message = "删除售后失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public int deleteByIds(Collection<Long> idList) {
        return 0;
    }

    @Override
    public void updateInfoById(Long id, AftermarketUpdateInfoParam aftermarketUpdateInfoParam) {
        log.debug("开始处理【修改售后详情】的业务，ID：{}，新数据：{}", id, aftermarketUpdateInfoParam);
       AftermarketStandardVO queryResult = aftermarketRepository.getStandardById(id);
        if (queryResult == null) {
            String message = "修改售后详情失败，尝试修改的售后数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }



        Aftermarket aftermarket = new Aftermarket();
        BeanUtils.copyProperties(aftermarketUpdateInfoParam,aftermarket);
        aftermarket.setId(id);
        int rows = aftermarketRepository.update(aftermarket);
        if (rows != 1) {
            String message = "修改售后详情失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public AftermarketStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询售后详情】的业务，参数：{}", id);
        AftermarketStandardVO queryResult = aftermarketRepository.getStandardById(id);
        if (queryResult == null) {
            String message = "查询类别详情失败，类别数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public List<AftermarketListItemVO> list() {
        log.debug("开始处理【获取售后列表】的业务，参数：无");
        List<AftermarketListItemVO> aftermarketList = aftermarketRepository.list();
        return aftermarketList;
    }
}
